package com.szholly.data.coderule.rules;

import com.szholly.utils.util.StringUtils;
import com.szholly.data.coderule.table.*;
import com.szholly.data.general.*;

class CodeRuleBase implements ICodeRule {

	public String GetCodeRule(ITable table, RowBase entity, String para,
			RowBase refEntity, String curCodeRule, String ruleInfoID) {
		return "";
	}

	public Boolean NeedCodeRule(ITable table, RowBase entity, String para,
			RowBase refEntity, String fields) {
		if (StringUtils.IsNullOrEmpty(fields))
			return false;
		if (entity == null)
			return false;
		return true;
	}

	public String Sort(ITable table, RowBase entity, String fieldName,
			String strWhere, long defaultValue, String ruleInfoID) {
		RuleValueRow rvRow = RuleValueTable.GetRowByIDAndStrWhere(ruleInfoID,
				strWhere);
		String strRuleValue = "";
		if (rvRow != null) {
			int nowValue = -1;

			// int.TryParse(rvRow.RULEVALUE, out nowValue);
			try {
				nowValue = Integer.parseInt(rvRow.getRULEVALUE());
			} catch (Exception e) {

			}
			if (nowValue < 0)
				return "";
			if (getValueType == GetValueType.Max) {
				nowValue = nowValue + 1;
			} else if (getValueType == GetValueType.Min) {
				nowValue = nowValue - 1;
			} else {
				throw new RuntimeException("编码规则：数据类型转换失败!");
			}
			strRuleValue = "" + nowValue;
			rvRow.setRULEVALUE(strRuleValue);
		} else {
			strRuleValue = Sort(table, fieldName, strWhere, defaultValue);
			rvRow = new RuleValueRow();
			ruleInfoID = ruleInfoID.substring(1, ruleInfoID.length() - 1);
			rvRow.setRULEINFOID(ruleInfoID.toString());
			rvRow.setRULEVALUE(strRuleValue);
			rvRow.setRULECONDITION(strWhere);
			if (table != null)
				rvRow.setTABLEID(table.getTableName());
		}
		RuleValueTable.getSingleRef().save(rvRow);
		return strRuleValue;
	}

	public String UnuseSort(ITable table, RowBase entity, String fieldName,
			String strWhere, long defaultValue, String ruleInfoID) {
		return Sort(table, fieldName, strWhere, defaultValue);
	}

	// / <summary>
	// / 获取查询字符串
	// / </summary>
	// / <param name="table">数据表</param>
	// / <param name="entity">实体类</param>
	// / <param name="groupFields">分组字段</param>
	// / <returns></returns>
	protected String GetSearchString(ITable table, RowBase entity,
			String groupFields) {
		String searchString = "";

		if (table == null || entity == null
				|| StringUtils.IsNullOrSpace(groupFields))
			return searchString;

		String[] pFields = groupFields.split(",");

		for (String field : pFields) {
			Object obj = entity.getValue(GlobalDataType.String, field);

			Field item = table.getField(field);
			if (item == null)
				continue;

			// 数据对象的最大值，和最小值不处理
			if (item.getDataType() == GlobalDataType.Date
					|| item.getDataType() == GlobalDataType.DateTime) {
				if (obj != null) {
					// if (obj.Equals(DateTime.MinValue)
					// || obj.Equals(DateTime.MaxValue))
					// continue;
				}
			}

			String strValue = DbValueConvertUtils.ConvertSearchValue(item, obj,
					null);
			if (!StringUtils.IsNullOrSpace(strValue))
				searchString += strValue + "and ";
		}

		if (!StringUtils.IsNullOrSpace(searchString)) {
			// searchString = searchString.Remove(searchString.length() - 5);
			searchString = searchString.substring(0, searchString.length() - 5);
		}

		return searchString;
	}

	// / <summary>
	// / 排序类型
	// / </summary>
	protected enum GetValueType {
		// / <summary>
		// / 最大值
		// / </summary>
		Max,
		// / <summary>
		// / 最小值
		// / </summary>
		Min,
		// / <summary>
		// / 未使用的最大值
		// / </summary>
		UnusedMax,
		// / <summary>
		// / 未使用的最小值
		// / </summary>
		UnusedMin
	}

	protected GetValueType getValueType;

	// / <summary>
	// / 排序
	// / </summary>
	private String Sort(ITable table, String fieldName, String strWhere,
			long defaultValue) {
		long longValue = -1;

		switch (getValueType) {
		case Max:
			longValue = table.getSpecFieldMaxValue(fieldName, strWhere,
					null);
			longValue++;
			break;
		case UnusedMax:
			break;
		case Min:
			longValue = table.getSpecFieldMinValue(fieldName, strWhere,
					null);
			longValue--;
			break;
		case UnusedMin:
//			longValue = table.getSpecFieldMinUnuseValue(fieldName,
//					strWhere, null);
			break;
		}

		return "" + longValue;
	}
}
